package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author stevejson
 */
public class MyClassLoader extends ClassLoader {
    private final String basePath;


    public MyClassLoader(String basePath) {
        this.basePath = basePath;
    }


    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        try {
            String fileName = basePath+"/"+"classes"+"/"+name.replaceAll("\\.","/")+ ".class";
            File file = new File(fileName);
            if(!file.exists()){
                return super.loadClass(name);
            }
            InputStream is = new FileInputStream(fileName);
            byte[] b = new byte[is.available()];
            is.read(b);
            return defineClass(name, b, 0, b.length);
        } catch (IOException e) {
            throw new ClassNotFoundException(name);
        }
    }


}
